<template>
  <div class="login-container">
    <div class="login-bg">
      <div class="login-bg_pattern">
        <div class="login-bg_left-top-circle" />
        <div class="login-bg_left100-top-circle" />
        <div class="login-bg_right-top-circle" />
        <div class="login-bg_right-bottom-circle" />
        <div class="login-bg_left-bottom-circle" />
      </div>
      <div class="login-login_wrap">
        <div class="login-login_box">
          <div class="el-row is-justify-center el-row--flex">
            <div

              class="el-col el-col-24 el-col-xs-22 el-col-sm-20 el-col-md-20 el-col-lg-20 el-col-xl-14"
            >
              <div class="grid-content">
                <div class="key">
                  <div class="bottom-img" />
                  <div class="form-info mt14vh pb120">
                    <div class="el-row">
                      <div style="padding: 0 5vw">

                        <el-form
                          ref="loginForm"
                          :model="loginForm"
                          :rules="loginRules"
                          class="login-form"
                          autocomplete="on"
                          label-position="left"
                        >

                          <div class="title-container">
                            <h3 class="title" />
                            <img src="~@/assets/img/logo.png" alt="">
                          </div>

                          <el-form-item prop="username">
                            <span class="svg-container">
                              <svg-icon icon-class="user" />
                            </span>
                            <el-input
                              ref="username"
                              v-model="loginForm.username"
                              placeholder="Username"
                              name="username"
                              type="text"
                              tabindex="1"
                              autocomplete="on"
                            />
                          </el-form-item>

                          <el-tooltip
                            v-model="capsTooltip"
                            content="Caps lock is On"
                            placement="right"
                            manual
                          >
                            <el-form-item prop="password">
                              <span class="svg-container">
                                <svg-icon icon-class="password" />
                              </span>
                              <el-input
                                :key="passwordType"
                                ref="password"
                                v-model="loginForm.password"
                                :type="passwordType"
                                placeholder="Password"
                                name="password"
                                tabindex="2"
                                autocomplete="on"
                                @keyup.native="checkCapslock"
                                @blur="capsTooltip = false"
                                @keyup.enter.native="handleLogin"
                              />
                              <span class="show-pwd" @click="showPwd">
                                <svg-icon
                                  :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
                                />
                              </span>
                            </el-form-item>
                          </el-tooltip>

                          <el-button
                            class="btn"
                            round
                            :loading="loading"
                            type="primary"
                            @click.native.prevent="handleLogin"
                          >登录</el-button>

                          <!-- 第三方登录 -->
                          <!-- <div style="position:relative">
                            <el-button class="thirdparty-button" type="primary" @click="showDialog=true">
                              Or connect with
                            </el-button>
                          </div> -->
                        </el-form>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 显示第三方登录插件 -->
    <!-- <el-dialog title="Or connect with" :visible.sync="showDialog">
      <social-sign />
    </el-dialog> -->
  </div>
</template>

<script>
// import SocialSign from './components/SocialSignin'

export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', messsage: '请输入用户名' }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.mt60 {
    margin-top: 60px;
}
.pb120 {
    padding-bottom: 120px;
}
.mt14vh {
    margin-top: 11vh;
    margin-bottom: 11vh;
}
li {
    list-style: none;
}
input {
    text-rendering: auto;
    color: -internal-light-dark-color(black, white);
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    background-color: -internal-light-dark-color(white, black);
    cursor: text;
    margin: 0em;
    font: 400 13.3333px Arial;
    padding: 1px 0px;
    border-width: 2px;
    border-style: inset;
    border-color: initial;
    border-image: initial;
}
.login-bg{
  background: #f0f7ff;
  width: 100vw;
  height: 100vh;
  position: relative;
  overflow: hidden;
  .login-bg_pattern .login-bg_left100-top-circle, .login-bg_pattern .login-bg_left-bottom-circle,
  .login-bg_pattern .login-bg_left-top-circle, .login-bg_pattern .login-bg_right-bottom-circle,
  .login-bg_pattern .login-bg_right-top-circle{
    opacity: .45;
    background: #e1eeff;
    border-radius: 74px;
    display: block;
    float: left;
    position: relative;
  }
  .login-bg_pattern {
    position: absolute;
    z-index: 1;
    width: 100vw;
    height: 100vh;
    .login-bg_left-top-circle{
        width: 300px;
        height: 300px;
        -webkit-transform: rotate(30deg);
        transform: rotate(30deg);
        margin-top: -150px;
        margin-left: 70px;
    }
    .login-bg_left100-top-circle{
        width: 200px;
        height: 200px;
        -webkit-transform: rotate(30deg);
        transform: rotate(30deg);
        margin-top: -150px;
        margin-left: 200px;
    }
    .login-bg_right-top-circle {
        width: 300px;
        height: 400px;
        margin-right: -100px;
        -webkit-transform: rotate(42deg);
        transform: rotate(42deg);
        float: right;
    }
    .login-bg_right-bottom-circle{
        width: 300px;
        height: 300px;
        -webkit-transform: rotate(30deg);
        transform: rotate(30deg);
        bottom: -200px;
        right: 350px;
        position: absolute;
    }
    .login-bg_left-bottom-circle {
        width: 300px;
        height: 300px;
        -webkit-transform: rotate(-11deg);
        transform: rotate(-11deg);
        bottom: -50px;
        left: -100px;
        position: absolute;
    }
  }

  .login-login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
    z-index: 1000;
    width: 100%;
    .key {
      box-sizing: border-box;
      background: #fff url('~@/assets/img/login/bg1.png') 100% 0 no-repeat;
      box-shadow: 0 0 60px 5px rgba(36,132,255,.41);
      border-radius: 8px;
      overflow: hidden;
      animation: fromBack-data-v-7a7a1ec8 .3s linear .1s forwards;

      .bottom-img {
        width: 170px;
        height: 170px;
        display: block;
        position: absolute;
        z-index: 2;
        bottom: 0;
        background: url('~@/assets/img/login/bg2.png') 0 100% no-repeat;
      }
      .form-info {
        .logo{
          width: 200px;
          height: 89px;
        }
        .goHome {
          background: #3e76d4;
          box-shadow: 0 12px 51px -12px #3376e4;
          border-radius: 100px;
          width: 140px;
          height: 40px;
          display: block;
          margin: 24px auto;
          font-size: 18px;
          text-align: center;
          line-height: 17px;
        }
        .user-info {
          padding-left: 30px;
          .user-input {
            position: relative;
            border-bottom: 1px solid #e0e0e0;
            padding: 0 20px 0 30px;
            margin-top: 37px;
            height: 30px;
            background: #fff;
            input {
              position: relative;
              width: 100%;
              font-size: 13px;
            }
            .effect~label {
              position: relative;
              top: -45px;
              width: 100%;
              color: #919191;
              transition: .3s;
              font-size: 12px;
              letter-spacing: .5px;
            }
          }
        }
      }
    }
  }

}

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 450px;
    max-width: 100%;
    padding: 0 35px 0;
    margin: 0;
    background-color: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
    .btn{
      width:100%;
      margin-bottom:30px;
      box-shadow: 0 12px 51px -12px #3376e4;
      padding: 12px 20px;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    margin-bottom: 35px;
    text-align: center;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
